package game.manager;

import game.Constants;
import game.Event;
import game.util.Util;

import java.util.Observable;
import java.util.Vector;

public class EventManager extends Observable implements Manager {
	private Vector<Event> events;

	private static EventManager manager;

	public static EventManager getManager() {
		if (manager == null)
			manager = new EventManager();
		return manager;
	}

	private EventManager() {
		this.initialize();
	}

	public void sendEvent(Event event) {
		this.events.add(event);
		this.setChanged();
	}

	@Override
	public void gameUpdate() {
		// Push queued events
		for (Event event : this.events) {
			if (Constants.LOG_EVENTS)
				logEvent(event);
			this.notifyObservers(event);
		}

		this.events.clear();
		this.clearChanged();
	}

	private void logEvent(Event event) {
		Util.log("Event " + event.getType() + " fired\n");
	}

	@Override
	public void update(Observable o, Object arg) {
	}

	@Override
	public void initialize() {
		this.events = new Vector<Event>();
	}
}
